package com.zztpt.mzb.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import com.zztpt.mzb.myapplication.entrity.Book;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
   ImageView imageView;
    //数组
    //int[] pics={R.mipmap.ic_launcher,R.mipmap.ic_launcher_round};
    private static final String TAG = "MainActivity";
    //连接两个线程的大师 handler
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg!=null){
                Bitmap bitmap= (Bitmap) msg.obj;//取对象
                //用对象
                imageView.setImageBitmap(bitmap);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //new一个对象，读取对象表   并引用数组
        //Book book=new Book(pics[0],1,"java",56f);
        init();//1.实例化控件
        //把线程放录子线程
        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                initData();//2.准备数据
            }
        }).start();//启动线程
                       // 显示当前线程                当前
        Log.d(TAG, "onCreate:   "+Thread.  currentThread().getName());
        //imageView.setImageResource(R.mipmap.timg);
        //imageView.setImageBitmap();
    }
    //方法       （）内有东西就是有参方法
    private void initData() {
        try {
            URL url=new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604471132316&di=b0e8789a0e00917b96ba5b009627e050&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201902%2F14%2F20190214112649_ygxfi.jpg");//封装类地址
            //得到网络连接了
            HttpURLConnection coon= (HttpURLConnection) url.openConnection();
            //设置访问方式get   post
            coon.setRequestMethod("GET");
            //判断服务器的返回结果
           int code=coon.getResponseCode();
         if(code==200){
           InputStream inputStream =coon.getInputStream();//读取网络通道
            Bitmap bitmap= BitmapFactory.decodeStream(inputStream);//流
              //往主线程中发消息
             //先obtain 拿出来  然后在放进去
              Message message= handler.obtainMessage();//连接主线程
              message.obj=bitmap;//放消息
             handler.sendMessage(message);//发信息给主线程
         }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {//   sast 处理openConnection异常
            e.printStackTrace();
        }
    }
    public  void init(){
        imageView=findViewById(R.id.lv_mzb);
    }

}
